package com.tupu.util;


import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class TokenUtils {

    //过期时间(1天)
    private static final long EXPIRE_TIME = 24 * 60 * 60 * 1000;

    //秘钥
    private static final String KEY = "cereshuzhitingnizhenbangcereshuzhitingnizhenbang";


    /**
     * 生成token
     *
     * @param id
     * @param claims
     * @return
     */
    public String generateToken(String id, Map<String, Object> claims) {
        Map<String, Object> header = new HashMap<>();
        header.put("alg", "HS256");
        header.put("type", "JWT");

        JwtBuilder builder = Jwts.builder().setHeader(header)
                .setClaims(claims)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE_TIME))
                .setSubject(id)
                .setIssuedAt(new Date())
                .signWith(SignatureAlgorithm.HS256, KEY);
        return builder.compact();
    }

}
